package sanrenxing.dayEnd.backRepertoryTotal.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sanrenxing.dayEnd.backRepertoryTotal.dao.TransferInstructionDao;
import sanrenxing.dayEnd.backRepertoryTotal.domain.CashAccount;
import sanrenxing.dayEnd.backRepertoryTotal.domain.TransferInstruction;
import sanrenxing.dayEnd.backRepertoryTotal.service.TransferInstructionBiz;

import java.util.List;

/**
 * @ClassName:TransferInstructionServiceImpl4
 * @description:孙祥兵
 * @date:2021/3/18 11:27
 * @version:1.0
 **/
@Service("/TransferInstructionBiz")
public class TransferInstructionServiceImpl implements TransferInstructionBiz {
    @Autowired
    private TransferInstructionDao transferInstructionDao;
    /**
     * 添加一行数据
     *
     * @param transferInstruction 划款对象
     * @return
     */
    @Override
    public int add(TransferInstruction transferInstruction) {
        return transferInstructionDao.add(transferInstruction);
    }

    /**
     * 删除一行数据
     *
     * @param tranId 划款编号
     * @return
     */
    @Override
    public int delete(int tranId) {
        return transferInstructionDao.delete(tranId);
    }

    /**
     * 修改一行数据
     *
     * @param transferInstruction 划款对象
     * @return
     */
    @Override
    public int update(TransferInstruction transferInstruction) {
        return transferInstructionDao.update(transferInstruction);
    }

    /**
     * 按条件分页查询
     *
     * @param rows      行数
     * @param pages     页数
     * @param condition 条件
     * @return
     */
    @Override
    public List<TransferInstruction> findAll(int rows, int pages, String condition) {
        int start=(pages-1)*rows;
        int row=pages*rows+1;
        return transferInstructionDao.findAll(start,row,condition);
    }

    /**
     * 按条件统计记录数
     *
     * @param condition 条件
     * @return
     */
    @Override
    public int count(String condition) {
        return transferInstructionDao.count(condition);
    }

    /**
     * 获取查询条件
     *
     * @param transferInstruction 划款对象
     * @return
     */
    @Override
    public String getCondition(TransferInstruction transferInstruction) {
        //定义查询条件字符串，初值为带空格的字符串
        String condition=" ";

        //划款日期
        if (transferInstruction.getTranDate()!=null){
            condition+=" and to_char(tranDate,'yyyy-MM-dd')='"+transferInstruction.getTranDate()+"'";
        }

        if (transferInstruction.getArrival()!=null){
            condition+=" and to_char(arrival,'yyyy-MM-dd')='"+transferInstruction.getArrival()+"'";
        }

        //如果调拨对象为空
        if (transferInstruction==null){
            return condition;
        }
        //如果划款编号不为0
        if (transferInstruction.getTranId()!=0){
            condition+=" and tranId=\'"+transferInstruction.getTranId()+"\'";
        }
        //如果接收账号不为0
        if (transferInstruction.getCaIds()!=0){
            condition+=" and caIds='"+transferInstruction.getCaIds()+"'";
        }
        //如果划款金额不为0
        if (transferInstruction.getMoney()!=0){
            condition+=" and money="+transferInstruction.getMoney();
        }
        //如果划款用途不为空
        if (transferInstruction.getPurpose()!=null && transferInstruction.getPurpose().trim().length()!=0){
            condition+=" and purpose like '%"+transferInstruction.getPurpose()+"%' ";
        }
        //如果划款账户不为0
        if (transferInstruction.getCaId()!=0){
            condition+=" and caId="+transferInstruction.getCaId();
        }
        return condition;
    }

    @Override
    public TransferInstruction selectById(String tranId) {
        return transferInstructionDao.selectById(tranId);
    }

    @Override
    public CashAccount selectCashById(String caId) {
        return transferInstructionDao.selectCashById(caId);
    }
}
